package com.itheima.store.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.store.domain.Book;
import com.itheima.store.domain.BookBorrow;
import com.itheima.store.domain.BookBorrowItems;
import com.itheima.store.domain.PageBean;
import com.itheima.store.domain.User;
import com.itheima.store.service.BookBorrowItemsService;
import com.itheima.store.service.BookBorrowService;
import com.itheima.store.service.BookCategoryService;
import com.itheima.store.service.BookService;
import com.itheima.store.utils.BaseServlet;
import com.itheima.store.utils.BeanFactory;
import com.itheima.store.utils.UUIDUtils;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class BookBorrowServlet
 */
public class BookBorrowServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
/**
 * 添加申请
 * @param req
 * @param resp
 * @return
 */
	public String addBookBorrow(HttpServletRequest req, HttpServletResponse resp){
		User user = (User) req.getSession().getAttribute("existUser");
		if(user == null){
			req.setAttribute("msg", "您还没有登录呦!");
			return "/jsp/login.jsp";
		}
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String books_id = req.getParameter("book_id");
		String user_id = user.getUser_id();
		String total = req.getParameter("total");
		String expeReturnTime1 = req.getParameter("expeReturnTime");
		Date expeReturnTime = null;
		try {
			 expeReturnTime = sdf.parse(expeReturnTime1);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		BookBorrow bookBorrow = new BookBorrow();
		try {
			bookBorrow = bbs.findByBookId(books_id,user_id);
		} catch (SQLException e3) {
			// TODO Auto-generated catch block
			e3.printStackTrace();
		}
		if(bookBorrow!=null){
			String bookBorrowing_id = bookBorrow.getBookBorrowing_id();
			Integer totals =bookBorrow.getTotal();
			Integer tot = totals+Integer.parseInt(total);
			try {
				bbs.updateTotal(bookBorrowing_id,tot,expeReturnTime);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "/jsp/cartforword.jsp";
		}else{
		
		BookService bookService = (BookService) BeanFactory.getBean("bookService");
		Book book = new Book();
		try {
			 book = bookService.findByBook_id(books_id);
		} catch (SQLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		String books_name = req.getParameter("book_name");
		

		//BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		String bookBorrowing_id = UUIDUtils.getUUID();
		String user_name = user.getUser_name();
		Date applicationTime = Calendar.getInstance().getTime();
		String time = sdf.format(applicationTime);
		try {
			applicationTime = sdf.parse(time);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//java.sql.Date applicationTime = new java.sql.Date(applicationTime1.getTime());
		Date confirmationTime = null;

		Date actuReturnTime = null;
		Integer isRenew = 0;
		Integer renewDays = 0;
		Integer bstatus = 3;
		BookBorrow bb = new BookBorrow();
		bb.setBookBorrowing_id(bookBorrowing_id);
		bb.setUser_id(user_id);
		bb.setUser_name(user_name);
		bb.setBooks_id(books_id);
		bb.setBooks_name(books_name);
		bb.setApplicationTime(applicationTime);
		bb.setConfirmationTime(confirmationTime);
		bb.setExpeReturnTime(expeReturnTime);
		bb.setActuReturnTime(actuReturnTime);
		bb.setIsRenew(isRenew);
		bb.setRenewDays(renewDays);
		bb.setBstatus(bstatus);
		bb.setTotal(Integer.parseInt(total));
		bb.setBook(book);
		bb.setBook_image(book.getBook_image());
		bb.setBook_author(book.getBook_author());
		bb.setInventory(book.getInventory());
		bb.setBook_number(book.getBook_number());
		String flag= "";
		try {
			flag = bbs.addBookBorrow(bb);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if("1".equals(flag)){
			return "/jsp/cartforword.jsp";
				//findApplication(req,resp);
				//resp.sendRedirect(req.getContextPath()+"/jsp/cart.jsp");
			
		}else{
			req.setAttribute("msg", "申请失败！请联系管理员！！！");
			return "/jsp/success.jsp";
		}
	}
		
	}
	
	/**
	 * 查询购物车列表
	 * @param req
	 * @param resp
	 * @return
	 */
	public String findApplication(HttpServletRequest req, HttpServletResponse resp){
		User user = (User) req.getSession().getAttribute("existUser");
		String user_id  = user.getUser_id();
		// 接收参数:					
		Integer currPage = Integer.parseInt(req.getParameter("currPage")==null?"1":req.getParameter("currPage"));
		// 调用业务层:
		BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		PageBean<BookBorrow> pageBean = null;
		try {
			pageBean = bbs.findAppByPageuserId(user_id,currPage);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		req.setAttribute("pageBean", pageBean);

		return "/jsp/cart.jsp";
		
	}
	
	/**
	 * 删除申请表
	 * @param req
	 * @param resp
	 * @return
	 */
	public String removeCart(HttpServletRequest req, HttpServletResponse resp){
		String bookBorrowing_id  = req.getParameter("bookBorrowing_id");
		BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		try {
			bbs.removeCart(bookBorrowing_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "/jsp/cartforword.jsp";
	}
	
	/**
	 * 预约书单的数量+ —
	 * @param req
	 * @param resp
	 * @return
	 */
	public String changeCart(HttpServletRequest req, HttpServletResponse resp){
		String bookBorrowing_id = req.getParameter("bookBorrowing_id");
		String flag = req.getParameter("ajax");
		String count = req.getParameter("count");
		BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		BookBorrow bb = new BookBorrow();
		try {
			bb = bbs.findAppBybookBorrId(bookBorrowing_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Integer total = bb.getTotal();
		if("reduce".equals(flag)) {
			total -=1;
			}else if("plus".equals(flag)) {
				total += 1;
			}
		try {
			resp.sendRedirect(req.getContextPath()+"/BookBorrowServlet?method=updateBookBorrow&bookBorrowing_id="+bookBorrowing_id+"&total="+total+"");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 更新借书数量
	 * @param req
	 * @param resp
	 * @return
	 */
	public String updateBookBorrow(HttpServletRequest req, HttpServletResponse resp){
		BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		String bookBorrowing_id = req.getParameter("bookBorrowing_id");
		String total = req.getParameter("total");
		try {
			bbs.updateBookBorrow(bookBorrowing_id,total);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 清空购物车
	 * @param req
	 * @param resp
	 * @return
	 */
	public String clearCart(HttpServletRequest req, HttpServletResponse resp){
		BookBorrowService bbs = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		User user = (User) req.getSession().getAttribute("existUser");
		String user_id = user.getUser_id();
		try {
			bbs.deleteBookBorrAllApp(user_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return "/jsp/cartforword.jsp";
	}
	
	//续借图书
	public String renewDay(HttpServletRequest req ,HttpServletResponse resp){
		String bookBorrowing_id = req.getParameter("bookBorrowing_id");
		//String[] bbids = bookBorrowing_id.split(",");
		String renewDays = req.getParameter("renewDays");
		//String idnum = req.getParameter("idnum");
		//String bbid = bbids[Integer.parseInt(idnum)];
		BookBorrowService bookBorrowService  = (BookBorrowService) BeanFactory.getBean("bookBorrowService");
		try {
			bookBorrowService.updateRenewDays(bookBorrowing_id,renewDays);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			resp.sendRedirect(req.getContextPath()+"/BookBorrowItemsServlet?method=findByUserId&currPage=1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;
	}
	
}
